local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator(modname)


-- [ Settings ] ---
-------------------

local step_interval = tonumber(minetest.settings:get(modname.."_step_interval")) or 0.15
local use_aux1 = minetest.settings:get_bool(modname.."_use_aux1",true)

if not minetest.global_exists("armor_monoid") then use_aux1 = false end

-- [ Craft ] ---
----------------

local item_none = ""
local item_silk = modname..":silk"

local item_strip = "basic_materials:steel_strip"
local item_string = "farming:string"
local item_spring = modname..":spring"
local item_feather = "mobs:chicken_feather"
local item_steel_ingot = "default:gold_ingot"
local item_gold_ingot = "default:gold_ingot"
local silk_colors = {}


local missing_items = function(items)
  local missing = {}
  for _,item in ipairs(items) do
    if not minetest.registered_items[item] then missing[item] = true end
  end
  if #missing > 0 then return missing end
  return false
end


-- --- [ Silk ] ---
--------------------

minetest.register_craftitem(item_silk, {
  description = S("Silk"),
  inventory_image = modname.."_silk.png"
})

minetest.register_alias('speed_boots:silk', item_silk)

if minetest.registered_items[item_string] then

  minetest.register_craft({
    output = item_silk,
    recipe = {
      {item_string, item_string},
      {item_string, item_string}
    }
  })

end

local register_colored_silk = function(color, description)

  local itemname = modname..":silk_"..color
  local dyename = "dye:"..color

  if minetest.registered_items[dyename] then
    minetest.register_craftitem(itemname, {
      description = description,
      inventory_image = modname.."_silk_"..color..".png"
    })

    minetest.register_craft({
      output = itemname,
        type = "shapeless",
      recipe = {
        item_silk,
        dyename
      }
    })
  end

  silk_colors[color] = itemname

end

-- register_colored_silk('blue', S("Blue Silk"))
-- register_colored_silk('red', S("Red Silk"))
-- register_colored_silk('green', S("Green Silk"))
-- register_colored_silk('black', S("Black Silk"))

minetest.register_craftitem(item_spring, {
  description = S("Spring"),
  inventory_image = modname.."_spring.png"
})

if not missing_items({item_strip}) then
  minetest.register_craft({
    output = item_spring..' 3',
    type = "shaped",
    recipe = {
      {item_strip},
      {item_strip},
      {item_strip}
    }
  })
elseif not missing_items({item_steel_ingot}) then
  minetest.register_craft({
    output = item_spring..' 18',
    type = "shaped",
    recipe = {
      {item_steel_ingot},
      {item_steel_ingot},
      {item_steel_ingot}
    }
  })
end

-- [ Items ] ---
----------------

-- --- [ Speed Boots ] ---
--------------------------

local armor_boots_speed = {}
armor_boots_speed.enabled = minetest.settings:get_bool(modname.."_speed_boot_enabled", true)
armor_boots_speed.multiplier = tonumber(minetest.settings:get(modname.."_speed_boot_multiplier")) or 1.75

if armor_boots_speed.enabled then

  armor_boots_speed.name = modname..":boots_speed"
  local boots_speed_multiplier = 0.3
  local boots_speed_on_press_aux1 = nil
  if not use_aux1 then
    boots_speed_multiplier = armor_boots_speed.multiplier
  end

  armor:register_armor(armor_boots_speed.name, {
    description = S("Boots of Swiftness"),
    inventory_image = modname.."_boots_speed_inv.png",
    groups = {
        armor_feet=1,
        armor_heal=0,
        armor_use=4000,
        physics_speed=boots_speed_multiplier,
        physics_jump=0.2
      },
    armor_groups = {fleshy=5},
    damage_groups = {cracky=3, snappy=3, choppy=2, crumbly=2, level=1},
  })

  if not silk_colors.blue then register_colored_silk('blue', S("Blue Silk")) end
  if not missing_items({silk_colors.blue,item_gold_ingot}) then

    minetest.register_craft({
      type = "shaped",
      output = armor_boots_speed.name,
      recipe = {
        {item_none, item_none, item_none},
        {silk_colors.blue, item_none, silk_colors.blue},
        {item_gold_ingot, item_none, item_gold_ingot}
      }
    })
  end

end

minetest.register_alias('speed_boots:boots_speed', armor_boots_speed.name)

-- --- [ Jump Boots ] ---
--------------------------

local armor_boots_jump = {}
armor_boots_jump.enabled = minetest.settings:get_bool(modname.."_jump_boot_enabled", true)
armor_boots_jump.multiplier = tonumber(minetest.settings:get(modname.."_jump_boot_multiplier")) or 1.25

if armor_boots_jump.enabled then

  armor_boots_jump.name = modname..":boots_jump"

  armor:register_armor(armor_boots_jump.name, {
    description = S("Bouncy Boots"),
    inventory_image = modname.."_boots_jump_inv.png",
    groups = {
        armor_feet=1,
        armor_heal=0,
        armor_use=2000,
        physics_speed=0.2,
        physics_jump=armor_boots_jump.multiplier
      },
    armor_groups = {fleshy=5},
    damage_groups = {cracky=3, snappy=3, choppy=2, crumbly=2, level=1},
  })

  if not silk_colors.green then register_colored_silk('green', S("Green Silk")) end
  if not missing_items({silk_colors.green,item_spring}) then

    minetest.register_craft({
      type = "shaped",
      output = armor_boots_jump.name,
      recipe = {
        {item_none, item_none, item_none},
        {silk_colors.blue, item_none, silk_colors.blue},
        {item_spring, item_none, item_spring}
      }
    })

  end

end

-- --- [ Feather Cape ] ---
---------------------------

local armor_cape_feather = {}
armor_cape_feather.enabled = minetest.settings:get_bool(modname.."_feather_cape_enabled", true)

if armor_cape_feather.enabled then

  armor_cape_feather.name = modname..":cape_feather"

  armor:register_armor(armor_cape_feather.name, {
    description = S("Feather cape"),
    inventory_image = modname.."_cape_feather_inv.png",
    groups = {
        armor_capes=1,
        physics_speed=0.1,
        physics_jump=0.2,
        armor_feather=1,
        armor_use=8000
      },
    armor_groups = {fleshy=5},
    damage_groups = {cracky=3, snappy=3, choppy=2, crumbly=2, level=1},
  })

  if not missing_items({item_feather}) then

    local group_feather = "group:feather"
    minetest.register_craft({
      type = "shaped",
      output = armor_cape_feather.name,
      recipe = {
        {group_feather, group_feather, item_none},
        {group_feather, group_feather, item_none},
        {group_feather, group_feather, item_none}
      }
    })

  end

end

-- --- [ Fly Cape ] ---
------------------------

-- local armor_cape_fly = {}
-- armor_cape_fly.enabled = tonumber(minetest.settings:get_bool(modname.."_fly_cape_enabled"), true)

-- if armor_cape_fly.enabled then

  -- armor:register_armor("3d_armor_flyswim:demo_cape", {
    -- description = "Someones Cape",
    -- inventory_image = "3d_armor_flyswim_demo_cape_inv.png",
    -- groups = {armor_capes=1, physics_speed=1, armor_use=1000},
    -- armor_groups = {fleshy=5},
    -- damage_groups = {cracky=3, snappy=3, choppy=2, crumbly=2, level=1},
    -- on_equip = function(player)
          -- local privs = minetest.get_player_privs(player:get_player_name())
          -- privs.fly = true
          -- minetest.set_player_privs(player:get_player_name(), privs)
          -- end,

    -- on_unequip = function(player)
          -- local privs = minetest.get_player_privs(player:get_player_name())
          -- privs.fly = nil
          -- minetest.set_player_privs(player:get_player_name(), privs)
          -- end,
  -- })

-- end



-- [ Global step function ] ---
-------------------------------

-- --- [ Particles ] ---
------------------------

-- local function spawn_sprint_particles (pos)
  -- if spawn_particles then

          -- local texture = "tnt_smoke.png"
          -- local glow = 0
          -- local acceleration = {x = 0, y = -9.8, z = 0}

          -- if playerstats.grounded and minetest.registered_nodes[node.name] then
              -- if minetest.registered_nodes[node.name].tiles and
              -- type(minetest.registered_nodes[node.name].tiles[1]) == "string" then
                  -- texture = minetest.registered_nodes[node.name].tiles[1]
              -- end
              -- if minetest.registered_nodes[node.name].liquidtype ~= "none" then
                  -- acceleration = {x = 0, y = 0, z = 0}
              -- end
              -- glow = minetest.registered_nodes[node.name].light_source or 0
          -- end

          -- minetest.add_particlespawner({
              -- amount = math.random(4, 8),
              -- time = 0.05,
              -- minpos = {x=-0.35, y=-0.4, z=-0.35},
              -- maxpos = {x=0.35, y=-0.4, z=0.35},
              -- minvel = {x=-0.25, y=1, z=-0.25},
              -- maxvel = {x=0.25, y=3, z=0.25},
              -- minacc = acceleration,
              -- maxacc = acceleration,
              -- minexptime = 1.5,
              -- maxexptime = 2.5,
              -- minsize = 0.3,
              -- maxsize = 1.25,
              -- collisiondetection = true,
              -- collision_removal = false,
              -- attached = playerstats.ref,
              -- texture = texture,
              -- glow = glow
          -- })
      -- end

  -- end
-- end

-- --- [ Globalstep ] ---
-------------------------

if use_aux1 then

  local sprint_timer = 0
  local pm = player_monoids
  minetest.register_globalstep(function(dtime)

      for _, player in ipairs(minetest.get_connected_players()) do

          sprint_timer = sprint_timer + dtime
          if sprint_timer < step_interval then return
          else sprint_timer = 0 end

          local keys = player:get_player_control()

          if keys.aux1 then

            local name = player:get_player_name()
            if armor.def[name].feather > 0 then
              local vel_y = player:get_velocity().y
              if vel_y < 0 and vel_y < 3 then
                vel_y = -(vel_y * 0.15)
                player:add_velocity({x = 0, y = vel_y, z = 0})
              end
            end

            local equipped = armor:get_weared_armor_elements(player)
            if equipped['feet'] == armor_boots_speed.name then
              pm.speed:add_change(player, armor_boots_speed.multiplier, modname.."_boots_speed_sprint")
              pm.jump:add_change(player, 1.1, modname.."_boots_speed_jump")
            end
          else
            pm.speed:del_change(player, modname.."_boots_speed_sprint")
            pm.jump:del_change(player, modname.."_boots_speed_jump")
          end

      end
  end)

end
